package model

import (
	"fresh-box-mgr/freshrpc/freshrpc"
	"gorm.io/gorm"
)

type Fresh struct {
	gorm.Model
	FreshName   string `gorm:"type:varchar(30)"`
	FreshImages string `gorm:"type:varchar(32)"`
	FreshNum    int    `gorm:"type:int"`
	ClassId     int    `gorm:"int"`
}

func (f *Fresh) FindFreshByFreshName(db *gorm.DB, name string) error {
	return db.Debug().Where("fresh_name=?", name).First(f).Error
}

func (f *Fresh) CreateFresh(db *gorm.DB) error {
	return db.Debug().Create(f).Error
}
func ListFresh(db *gorm.DB, in *freshrpc.ListFreshRequest) []*freshrpc.ListFresh {
	var fresh []*freshrpc.ListFresh
	db.Debug().Model(&Fresh{}).Select("freshes.fresh_name AS fresh_name",
		"freshes.fresh_images AS fresh_images",
		"freshes.fresh_num AS fresh_num",
		"classes.class_name AS class_name").
		Joins("LEFT JOIN classes ON classes.id = freshes.class_id").
		Scopes(Paginate(int((in.Page)), int((in.Size)))).Find(&fresh)
	return fresh
}
